/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AdminConnect.java
 *
 * Created on 6 oct. 2011, 11:38:28
 */
package Vue;

import java.sql.*;
import java.awt.event.KeyEvent;

/**
 * @author Babs
 */
public class AdminConnect extends javax.swing.JDialog {

    /** Creates new form AdminConnect */
    public AdminConnect(Main parent, boolean modal) {
        super(parent, modal);
        initComponents();
        this.parent = parent;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        TFlogin = new javax.swing.JTextField();
        Ltitle = new javax.swing.JLabel();
        Llogin = new javax.swing.JLabel();
        Lidentif = new javax.swing.JLabel();
        Lpassword = new javax.swing.JLabel();
        BtnOk = new javax.swing.JButton();
        TFpassword = new javax.swing.JPasswordField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("VOilA - Identification");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosed(java.awt.event.WindowEvent evt) {
                formWindowClosed(evt);
            }
        });

        TFlogin.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                TFloginKeyTyped(evt);
            }
        });

        Ltitle.setFont(new java.awt.Font("Aston-F1", 0, 24));
        Ltitle.setText("Welcome on the vip management software");

        Llogin.setText("Login");

        Lidentif.setFont(new java.awt.Font("Aston-F1", 0, 20));
        Lidentif.setText("PLEASE identify yourself");

        Lpassword.setText("Password");

        BtnOk.setText("Ok");
        BtnOk.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BtnOkActionPerformed(evt);
            }
        });

        TFpassword.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                TFpasswordKeyTyped(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(85, 85, 85)
                        .addComponent(Ltitle))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(182, 182, 182)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(Lpassword)
                            .addComponent(Llogin))
                        .addGap(37, 37, 37)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(TFpassword)
                            .addComponent(TFlogin, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(Lidentif, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(BtnOk, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                .addContainerGap(58, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(49, 49, 49)
                .addComponent(Ltitle)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 43, Short.MAX_VALUE)
                .addComponent(Lidentif)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(TFlogin, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(Llogin))
                .addGap(17, 17, 17)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(Lpassword)
                    .addComponent(TFpassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(26, 26, 26)
                .addComponent(BtnOk)
                .addGap(24, 24, 24))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void formWindowClosed(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosed
        System.exit(0);
    }//GEN-LAST:event_formWindowClosed

    private void BtnOkActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtnOkActionPerformed
        tryToConnect();
    }//GEN-LAST:event_BtnOkActionPerformed

    private void TFpasswordKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_TFpasswordKeyTyped
        if (evt.getKeyChar() == 10) {
            tryToConnect();
        }
    }//GEN-LAST:event_TFpasswordKeyTyped

    private void TFloginKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_TFloginKeyTyped
        if (evt.getKeyChar() == 10) {
            tryToConnect();
        }
    }//GEN-LAST:event_TFloginKeyTyped
    private void tryToConnect() {
        login = TFlogin.getText().toLowerCase();
        pass = TFpassword.getText().toLowerCase();

        Bdd.AdminRepository adminRepo = new Bdd.AdminRepository();
        int repoResult = adminRepo.tryToConnect(login, pass);

        switch (repoResult) {
            case Bdd.AdminRepository.CONNECTION_OK:
                this.setVisible(false);
                parent.startApp();
                break;
            case Bdd.AdminRepository.CONNECTION_LOG_ERROR:
                javax.swing.JOptionPane.showMessageDialog(this, "Mot de passe éronné", "Erreur mot de passe", javax.swing.JOptionPane.ERROR_MESSAGE);
                break;
            case Bdd.AdminRepository.CONNECTION_MP_ERROR:
                javax.swing.JOptionPane.showMessageDialog(this, "Administrateur " + login + " inconnu", "Erreur login", javax.swing.JOptionPane.ERROR_MESSAGE);
                break;
            case Bdd.AdminRepository.CONNECTION_NET_ERROR:
                javax.swing.JOptionPane.showMessageDialog(this, "Echec de la connexion avec le serveur.", "Erreur connexion", javax.swing.JOptionPane.ERROR_MESSAGE);
                break;
            default:
                javax.swing.JOptionPane.showMessageDialog(this, "Impossible de se connecter", "Erreur inconnue", javax.swing.JOptionPane.ERROR_MESSAGE);
        }
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton BtnOk;
    private javax.swing.JLabel Lidentif;
    private javax.swing.JLabel Llogin;
    private javax.swing.JLabel Lpassword;
    private javax.swing.JLabel Ltitle;
    private javax.swing.JTextField TFlogin;
    private javax.swing.JPasswordField TFpassword;
    // End of variables declaration//GEN-END:variables
    private String login, pass;
    private Main parent;
}
